//
//  HRHomeViewController.swift
//  微博
//
//  Created by 众力金融 on 2017/1/5.
//  Copyright © 2017年 众力金融. All rights reserved.
//

import UIKit

fileprivate let cellId = "cellId"

class HRHomeViewController: HRBaseViewController {

    fileprivate lazy var listViewModel = HRStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       view.backgroundColor = UIColor.cz_random()
       //setupNav()
        
    }
    
   @objc fileprivate func leftclick() {
    
    print(#function)
      let vc = HRTestDemoViewController()
    navigationController?.pushViewController(vc, animated: true)
    
    }
    
    override func loadData() {
        
      
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, hasMorePullup) in
          
            print("刷新表格结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标志
            self.isPullup = false
            //刷新表格
            if hasMorePullup {
                self.tableview?.reloadData()
            }
            
        }
        
//        //模拟延时加载数据 -> dispatch_after
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
//
//        }
        
    }

}

//MARK: - 表格数据源方法
extension HRHomeViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //1.去cell
        let cell = tableview?.dequeueReusableCell(withIdentifier:cellId, for: indexPath)
        //2.设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text
        //3.返回cell
        return cell!
    }
}

// MARK: - 导航视图
extension HRHomeViewController {
  
//   fileprivate func setupNav() {
//    
//    navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(leftclick))
//    
//    tableview?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
//    
//    }
    override func setupTableView() {
        super.setupTableView()
        
         navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(leftclick))
        tableview?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        setupNavTitle()
    }
    //设置导航栏标题
    fileprivate func setupNavTitle() {
        
        let title = HRNetworkManager.shared.userAccount.screen_name
        let buttton = HRTitleButton(title: title)
        buttton.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        navItem.titleView = buttton
    }
    
    @objc func clickTitleButton(btn: HRTitleButton) {
        //设置选择状态
        btn.isSelected = !btn.isSelected
    }

}
